package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public IPage<RoomItemVo> findPageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findPageItem(page,queryVo);
    }
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        if(roomSubmitVo.getId() != null){

            Long roomId = roomSubmitVo.getId();

            deletedRoomAssociatedById(roomId);
        }
        this.saveOrUpdate(roomSubmitVo);

        long roomId = roomSubmitVo.getId();

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(graphVoList != null && graphVoList.size() > 0){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            graphInfoList.forEach(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(graphVo.getItemType());
                graphInfoList.add(graphInfo);
            });
                graphInfoService.saveBatch(graphInfoList);
        }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(attrValueIds != null && attrValueIds.size() > 0){
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
            attrValueIds.forEach(attrValueId -> {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                .attrValueId(attrValueId)
                        .roomId(roomId)
                        .build();
                roomAttrValueList.add(roomAttrValue);
            });
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(facilityInfoIds != null && facilityInfoIds.size() > 0){
            List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
            facilityInfoIds.forEach(facilityInfoId -> {
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityInfoId)
                        .roomId(roomId)
                        .build();
                roomFacilityList.add(roomFacility);
            });
            roomFacilityService.saveBatch(roomFacilityList);
        }

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(labelInfoIds != null && labelInfoIds.size() > 0){
            List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
            labelInfoIds.forEach(labelInfoId -> {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId)
                        .roomId(roomId)
                        .build();
                roomLabelList.add(roomLabel);
            });
            roomLabelService.saveBatch(roomLabelList);
        }

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(paymentTypeIds != null && paymentTypeIds.size() > 0){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
            paymentTypeIds.forEach(paymentTypeId -> {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(roomId)
                        .build();
                roomPaymentTypeList.add(roomPaymentType);
            });
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(leaseTermIds != null && leaseTermIds.size() > 0){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(leaseTermIds.size());
            leaseTermIds.forEach(leaseTermId -> {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId)
                        .roomId(roomId)
                        .build();
                roomLeaseTermList.add(roomLeaseTerm);
            });
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    @Override
    public RoomDetailVo getDetailById(Long roomId) {
        RoomDetailVo roomdetailVo = new RoomDetailVo();
        RoomInfo roomInfo = this.getById(roomId);
        BeanUtils.copyProperties(roomInfo, roomdetailVo);

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomdetailVo.setApartmentInfo(apartmentInfo);

        List<GraphVo> graphList = graphInfoMapper.findGraphListByItemId(ItemType.ROOM, roomId);
        roomdetailVo.setGraphVoList(graphList);

        List<AttrValueVo> attrValueVoList = attrValueMapper.findList(roomId);
        roomdetailVo.setAttrValueVoList(attrValueVoList);

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findList(roomId);
        roomdetailVo.setFacilityInfoList(facilityInfoList);

        List<LabelInfo> labelInfoList = labelInfoMapper.findList(roomId);
        roomdetailVo.setLabelInfoList(labelInfoList);

        List<PaymentType> paymentTypeList = paymentTypeMapper.findList(roomId);
        roomdetailVo.setPaymentTypeList(paymentTypeList);

        List<LeaseTerm> leaseTermList = leaseTermMapper.findList(roomId);
        roomdetailVo.setLeaseTermList(leaseTermList);
        return roomdetailVo;
    }

    @Override
    public void deletedRoomById(Long roomId) {
        this.deletedRoomAssociatedById(roomId);
        roomInfoMapper.deleteById(roomId);
    }

    @Override
    public List<RoomInfo> getRoomById(Long id) {
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        return roomInfoMapper.selectList(queryWrapper);
    }

    public void deletedRoomAssociatedById(Long roomId){
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemId,roomId)
                .eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(queryWrapper);

        LambdaQueryWrapper<RoomAttrValue> rqueryWrapper = new LambdaQueryWrapper<>();
        rqueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
        roomAttrValueService.remove(rqueryWrapper);

        LambdaQueryWrapper<RoomFacility> fqueryWrapper = new LambdaQueryWrapper<>();
        fqueryWrapper.eq(RoomFacility::getRoomId, roomId);
        roomFacilityService.remove(fqueryWrapper);

        LambdaQueryWrapper<RoomLabel> lqueryWrapper = new LambdaQueryWrapper<>();
        lqueryWrapper.eq(RoomLabel::getRoomId, roomId);
        roomLabelService.remove(lqueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> pqueryWrapper = new LambdaQueryWrapper<>();
        pqueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
        roomPaymentTypeService.remove(pqueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> lequeryWrapper = new LambdaQueryWrapper<>();
        lequeryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
        roomLeaseTermService.remove(lequeryWrapper);
    }


}




